<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拖拽</title>
    <style>
        *{
            padding:0;
            margin:0;
        }
        html,body,.box{
            width: 100%;
            height:100%;
            overflow: hidden;
        }
        .box{
            position: relative;
        }

        .shadow{
            width:100px;
            height: 100px;
            background: orange;
            /* border-radius: 50%; */
            position: absolute;
            top:50px;
            left:50px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    
    <div class="shadow"></div>
    <script>

        // 元素拖拽
        // 1. 先按下
        // 2. 鼠标滑动  改变对应的位置  left top
        // 3. 鼠标离开   松开  不再移动
        var shadow = document.getElementsByClassName('shadow')[0];
        var clientWidth = document.documentElement.clientWidth || document.body.clientWidth;
        var clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
        var maxLeft = clientWidth - shadow.clientWidth;    // 可以拖拽的 宽度
        var maxTop = clientHeight - shadow.clientHeight;  // 可以拖拽的 高度 

        // dragStart  x,y 这2个变量传递给 dragMove
        // dragMove
        // dragEnd
        var drag = {
            target:document.getElementsByClassName("shadow")[0],
            x:0,
            y:0,
            dragStart(){
                // this -> 对象 
                
                this.target.onmousedown = function(e){
                    console.log(this)   //  搞清 this 的指向问题   this =>  this.target
                    var e = e || window.event;
                    this.x = e.clientX - this.target.offsetLeft;
                    this.y = e.clientY - this.target.offsetTop;
                   
                     // 3. 鼠标离开   松开  不再移动
                    this.dragMove();
                    this.dragEnd();

                    // 强制把函数里的 this 指向 外面的this (箭头函数 )
                }.bind(this)
            },
            dragMove(){
                document.onmousemove = function(e){
                    var e = e || window.event;
                    var left = e.clientX -this.x;
                    var top = e.clientY - this.y;

                    if(left<0) left = 0;
                    if(left>maxLeft) left = maxLeft;
                    if(top<0) top = 0;
                    if(top>maxTop) top = maxTop;
                    
                    this.target.style.top = top +'px';
                    this.target.style.left = left +'px';
                }.bind(this)
            },
            dragEnd(){
                document.onmouseup =  function(){
                    document.onmousemove = null;
                    document.onmouseup = null;
                }
            }
        }

        drag.dragStart()



        // 换成构造函数 
    </script>
</body>
</html>